{% block content %}
{% load files %}
{% if TargetCategory or TargetUser or TargetAssignment %}
	<span class="floatleft">
	    <a href="{% course_url management app='grades' command='manage' %}">{{course}} Grades</a>
		> <a href="{% course_url management app='grades' command='manage' arguments=TargetCategory.slug %}">{{TargetCategory.title|safe}}</a>
			> <a href="{% course_url management app='grades' command='manage' arguments=TargetCategory.slug|add:"&&"|add:TargetAssignment.slug %}">{{TargetAssignment.title|safe}}</a>
	    {% if TargetUser %}
			> {{TargetUser.first_name}}&nbsp;{{TargetUser.last_name}}
	    {% endif %}
		> Upload
	</span>
{% endif %}
{% autoescape off %}
<div class="form_container">
    {# Floating Save Button #}
    
    {# Title #}
		<h1>
			<a class="AddButton button floatleft" href="{% course_url management app='grades' command='edit' arguments=TargetCategory.title %}" title="Add a new directly-entered grade to {{TargetCategory.title}}">+</a>
			Grades
		</h1>
		<div class="clear"></div>
		{% if Categories and Grades and Metrics %}
			<table class="grade_table left_justify" style="width: 1px;" border="1" cellpadding="2">
				<tr>
					<th></th>
					{% for category_name in CategoryIndex %}
						{% with category=Categories|getitem:category_name %}
							{% for assignment,sections in category.items %}
								{% if assignment != "info" %}
									{% with assignment_info=sections.info %}
										{% for section in sections %}
											{% if section != 'info' %}
												<th>
													{% if section.get_question %}
														<div class="popup_container">
															<span class="popup_title"><a>{{section.title}}</a></span>
															<span class="right_down_popup">{% render section.get_question.question %}</span>
														</div>
													{% else %}
														{{section.title}}<br>
													{% endif %}
													{{section.total_points}} Points
												</th>
											{% endif %}
										{% endfor %}
									{% endwith %}
								{% endif %}
							{% endfor %}
						{% endwith %}
					{% endfor %}
					<th>Total</th>
				</tr>
				{% for student in UserIndex %}
					{% with grades=Grades|getitem:student %}
						{% with StudentGradingForm=GradingForms|getitem:student %}
							<tr>
								<td width=1>
									<a name="{{student.last_name|slugify}}_{{student.first_name|slugify}}"></a>
									{% if AnonymousGrading and not TargetUser %}
										Student&nbsp;{{forloop.counter}}
									{% else %}
										{% if grades.link %}
											<a href="{% course_url management app='grades' command='manage' arguments=grades.link|concat:"&upload" %}">{{student.last_name}},&nbsp;{{student.first_name}}</a>
										{% else %}
											{{student.last_name}},&nbsp;{{student.first_name}}
										{% endif %}
									{% endif %}
								</td>
								{% for category_name in CategoryIndex %}
									{% with assignments=grades|getitem:category_name %}
										{% for assignment,sections in assignments.items %}
											{% if assignment != 'info' %}
												{% for section in sections %}
													{% if section != 'info' %}
														{% with GradingForm=StudentGradingForm|getitem:section %}
															{% with response=GradingForm.get_response %}
																<td>
																	<table>
																		<tr>
																			<td>
																				{{GradingForm.instance.points}} points
																				{% get_file_list for GradingForm.instance as Files %}
																				{% if GradingForm.instance.notes %}
																					<span class="popup_container">
																						<span class="popup_title">(Notes)</span>
																						<span class="right_over_popup">
																							<span class="popup_content">
																							Notes:<br>
																							{{GradingForm.instance.notes}}
																							</span>
																						</span>
																					</span>
																				{% endif %}
																					<hr>
																				<div class="expansion_container">
																					<div class="expansion_title">
																						<span class="arrow_down"></span>{% if Files %}Files&nbsp;({{Files|length}}){% else %}Add Files{% endif %}
																					</div>
																					<div class="expansion_content">
																						{% if Files %}
																								<ul>
																									{% for file in Files %}
																										<form action="{% course_url management app='files' command='delete' arguments=file.id %}" method="post" name="form{{file.id}}">{% csrf_token %}
																										<input type="hidden" name="next" id="next" value="{{request.path}}#{{student.last_name|slugify}}_{{student.first_name|slugify}}" />
																										<li>
																											<button type="submit" class="button DeleteButton smallbutton" name="DeleteFile" value="{{ file.id }}"  title="Delete This File">X</button><a href="{{file.url}}">{{file.title}}&nbsp;({{file.extension}})</a>{% if file.description %} - {{file.description}}{% endif %}
																										</li>
																										</form>
																									{% endfor %}
																								</ul>
																						{% endif %}
																						{% render_file_form for GradingForm.instance %}
																						<div class="expansion_footer"><span class="arrow_up"></span></div>
																					</div>
																				</div>
																				<br>{{GradingForm.points.errors}}
																				{% if response != "__None" %}
																					{{response}}
																				{% endif %}
																			</td>
																		</tr>
																	</table>
																</td>
															{% endwith %}
														{% endwith %}
													{% endif %}
												{% endfor %}
												<td>
													{{sections.info.percent|floatformat:"2"}}%
												</td>
											{% endif %}
										{% endfor %}
									{% endwith %}
								{% endfor %}
							</tr>
						{% endwith %}
					{% endwith %}
				{% endfor %}
				<tr>
					<td></td>
					{% for category in CategoryIndex %}
						{% with metrics=Metrics|getitem:category %}
							{% for assignment,sections in metrics.items %}
								{% if assignment != 'info' %}
									{% for section,info in sections.items %}
										{% if section != 'info' %}
											<td><table>
											<tr><td>Min:</td><td>{{info.min|floatformat:"2"}}</td>
											<tr><td>Max:</td><td>{{info.max|floatformat:"2"}}</td>
											<tr><td>Average:</td><td>{{info.average|floatformat:"2"}}</td>
											<tr><td>Median:</td><td>{{info.median|floatformat:"2"}}</td>
											</table></td>
										{% endif %}
									{% endfor %}
									<td>
										<table>
											<tr><td>Min:</td><td>{{sections.info.min|floatformat:"2"}}%</td></tr>
											<tr><td>Max:</td><td>{{sections.info.max|floatformat:"2"}}%</td></tr>
											<tr><td>Average:</td><td>{{sections.info.average|floatformat:"2"}}%</td></tr>
											<tr><td>Median:</td><td>{{sections.info.median|floatformat:"2"}}%</td></tr>
										</table>
									</td>
								{% endif %}
							{% endfor %}
						{% endwith %}
					{% endfor %}
				</tr>
			</table>
			<table>
				<tr>
					<td colspan="100%">
						<a class="button" href="{% course_url management app='grades' command='manage' arguments=TargetAssignment.link %}" title="Return to grading this assignment">Return to Assignment</a>
					</td>
				</tr>
			</table>
		{% else %}
			<p>There are no grades for this course</p>
		{% endif %}
    </form>
</div>
{% endautoescape %}
{% endblock content %}